package com.yangyang.msa.service;

import com.yangyang.msa.entity.User;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenshunyang
 * @version 1.0
 * @date 2021/8/27 4:21 下午
 */
@Service
public class UserService {
    private static Map<Long,User> usersMap = new HashMap<>();

    public List<User> findUserList() {
        User user1 = new User(1L, "account1", "张三", 20, new BigDecimal(100.00));
        User user2 = new User(2L, "account2", "李四", 28, new BigDecimal(180.00));
        User user3 = new User(3L, "account3", "王五", 32, new BigDecimal(280.00));
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        usersMap = users.stream().collect(Collectors.toMap(User::getId,a->a,(k1,k2)->k1));
        return users;
    }

    public User findUserById(Long userId) {
        return usersMap.get(userId);
    }

    public void saveUser(User user) {
        usersMap.put(user.getId(),user);
    }
}
